package com.ccrfid.rmc6.resource.video;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.video.VideoPlatformEntity;
import com.ccrfid.rmc6.pojo.dto.VideoPlatformDto;
import com.ccrfid.rmc6.service.video.VideoPlatformService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

@Path("/video/platforms")
@Produces(MediaType.APPLICATION_JSON)
public class VideoPlatformResource {
    private static final Logger logger = LoggerFactory.getLogger(VideoPlatformResource.class);
    @Context
    private HttpServletRequest httpServletRequest;
    @Autowired
    VideoPlatformService videoPlatformService;

    @GET
    public List<VideoPlatformEntity> getAllVideoPlatforms() {
        return videoPlatformService.getAllVideoPlatforms();
    }

    @GET
    @Path("{id}")
    public VideoPlatformEntity getVideoPlatform(@PathParam("id") Long id) {
        VideoPlatformEntity entity = videoPlatformService.getVideoPlatformById(id);
        return entity;
    }

    @GET
    @Path("{id}/linkage-info")
    public VideoPlatformDto getVideoPlatfromLinkageInfo(@PathParam("id") Long platformId) {
        return videoPlatformService.getVideoPlatfromLinkageInfo(platformId, httpServletRequest.getRemoteAddr());
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public VideoPlatformEntity createVideoPlatform(VideoPlatformEntity entity) throws AppException {
        VideoPlatformEntity videoPlatformEntity = videoPlatformService.createVideoPlatform(entity);
        return videoPlatformEntity;
    }

    @PUT
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public VideoPlatformEntity updateVideoPlatform(@PathParam("id") Long id, VideoPlatformEntity entity)
            throws AppException {
        VideoPlatformEntity videoPlatformEntity = videoPlatformService.updateVideoPlatform(id, entity);
        return videoPlatformEntity;
    }

    @DELETE
    @Path("{id}")
    public Boolean deleteVideoPlatform(@PathParam("id") Long id) throws AppException {
        if (id == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return videoPlatformService.deleteVideoPlatform(id);
    }

    @PUT
    @Path("{id}/sync-platform")
    public Boolean syncPlatformNode(@PathParam("id") Long platformId) throws AppException {
        return videoPlatformService.syncPlatformNode(platformId);
    }
}
